pimpas.XML = Class.create( );
pimpas.Extends(pimpas.XML.prototype, pimpas.Object);
pimpas.Extends(pimpas.XML.prototype, 
   {
	   xmlObject: null,
	   resolver: null,
	   evalBrowser: null,
	   getAsTextBrowser: null,
	   getAttributeValueBrowser: null,
	   initialize: function(xml) {
			this.id = pimpas.globalID++;
			this.log = new pimpas.Log( );
	   		this.xmlObject = xml;
			if(!pimpas.Browser.isIE) {
				this.resolver = this.xmlObject.createNSResolver( this.xmlObject.ownerDocument == null ? this.xmlObject.documentElement : this.xmlObject.																																						ownerDocument.documentElement);
				this.evalBrowser = this.evalMozilla;
				this.getAsTextBrowser = this.getAsTextMozilla;
				this.getAttributeValueBrowser = this.getAttributeValueMozilla;
			} else {
				this.evalBrowser = this.evalIE;
				this.getAsTextBrowser = this.getAsTextIE;
				this.getAttributeValueBrowser = this.getAttributeValueIE;
			}
	   },
	   eval: function(xpath, el) {
			if(!(xpath instanceof pimpas.XML.Xpath)) {
				this.log.log(pimpas.Log.ERROR, "Xpath need be child of class pimpas.XML.Xpath");
				return;
			}
			
			return this.evalBrowser.bind(this, (el!=undefined && el!=null) ? el : this.xmlObject, xpath)();
	   },
	   evalMozilla: function(el, xpath) {
		    return new pimpas.XML.MozillaIterator(this.xmlObject.evaluate(xpath.value, el, this.resolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ));
	   },
	   evalIE: function(el, xpath) {
	   		return new pimpas.XML.IEIterator(el.selectNodes(xpath.value));
	   },
	   getAsText: function(el) {
		   return this.getAsTextBrowser.bind(this, el)();
	   },
	   getAsTextMozilla: function(el) {
		   return el.textContent;
	   },
	   getAsTextIE: function(el) {
		   return el.text;
	   },
	   getAttributeValue: function(el, name) {
		   return this.getAttributeValueBrowser.bind(this, el, name)();
	   },
	   getAttributeValueMozilla: function(el, name) {
		   return el.getAttribute(name);
	   },
	   getAttributeValueIE: function(el, name) {
		   return el.getAttribute(name);
	   },
	   toString: function() {
			try {
				return (pimpas.Browser.isMozilla) ? (new XMLSerializer()).serializeToString(this.xmlObject) : object;
			} catch(e) {
				return object.toString( );	
			}
	   }
   }
);
pimpas.XML.Iterator = Class.create( );
pimpas.Extends(pimpas.XML.Iterator.prototype,
   {
	   initialize: function(r) {
		   this.result = r;
	   },
	   iterateNext: function() {
		   return null;
	   },
	   hasNext: function() {
	   	   return false;  
	   },
	   toString: function() {
	   		return "[XML ITERATOR]";   
	   }
   }
);
pimpas.XML.MozillaIterator = Class.create( );
pimpas.XML.IEIterator = Class.create( );
pimpas.Extends(pimpas.XML.MozillaIterator.prototype, pimpas.XML.Iterator.prototype);
pimpas.Extends(pimpas.XML.MozillaIterator.prototype,
   {
	   iterateNext: function() {
			return this.result.iterateNext( );   
	   },
	   hasNext: function() {
	   }
   }
);
pimpas.Extends(pimpas.XML.IEIterator.prototype, pimpas.XML.Iterator.prototype);
pimpas.Extends(pimpas.XML.IEIterator.prototype,
   {
	   pos: null,
	   count: null,
	   initialize: function(r) {
		   this.result = r;
		   this.pos = 0;
		   this.count = this.result.length;
	   },
	   hasNext: function() {
	   		return this.pos<this.count;
	   },
	   iterateNext: function() {
		   if(!this.hasNext()) return null;
		   var el = this.result[this.pos++];
		   return el;
	   }
   }
);